package ipsite.server;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;

import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;

import ipsite.data.Data;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import java.util.*;

public class AlexaUpdate extends Thread{
	DBObject domain;
	public AlexaUpdate(DBObject domain)
	{
		this.domain=domain;
		this.start();
	}
	public void run()
	{
		try {
			URL dataURL = new URL("http://www.alexa.com/siteinfo/"+domain.get("name"));
	
			BufferedReader reader = new BufferedReader(new InputStreamReader(dataURL.openStream(), Charset.forName("utf-8")));
			String content = "";
			char[] buf = new char[102102];
			int numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
	
			Document doc = Jsoup.parse(content);

			// visitor
			String visit = "0";
			if(doc.select(".visitors_percent").select(".avg").size() > 0)
			{
				visit = doc.select(".visitors_percent").select(".avg").last().text().trim();
			}
			// rank
			String ranks = null;
			if(doc.select("#siteStats").select("div").size() > 0)
			{
				ranks = doc.select("#siteStats").select("div").first().text().trim().replace(",","");
			}
			int rank = 0;
			
			try {
				if(ranks != null && !ranks.equals(""))
				{
					rank=Integer.parseInt( ranks );
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			
			
			//keywords
			ArrayList<String> b_key = new ArrayList<String>();
			Elements elements = doc.select("#top-keywords-from-search");

			if(elements.size() > 0)
			{
				for (Element element : elements.first().select("u")) {
					
					b_key.add(element.text().trim());
				}
			}
			
//			String speed = doc.select(".speedAd").select("p").first().text().replaceAll("[^(]+[(]+", "").replaceAll("[)]+[\\w\\W]+", "");
			
//			BasicDBObject info = new BasicDBObject();
//  			
//			DBCollection coll = Data.data("domain");
			domain.put("visit", visit);
			domain.put("lpKeyword", b_key);
			domain.put("rank", rank);
//			info.put("speed", speed);

//			BasicDBObject set = new BasicDBObject("$set", info);
//			coll.update(domain, set);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
//	public static void main( String args[])
//	{
//		
//		Domain domain = new Domain();
//		domain.setName("google.com");
//		new AlexaUpdate(domain);
//	}
	
}
